﻿using AutoMapper;
using B.S.DataBase.Myproject.Helper;
using B.S.DataBase.MyProject.Domain.wdx;
using B.S.DataBase.MyProject.Domain.wdx.ViewModels;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using Dm.util;
using MediatR;
using MedicalPCTerminal.Read.API.Appcation.Commands.居民档案PC端;
using System.Net;

namespace MedicalPCTerminal.Read.API.Appcation.CommandHandlers.居民档案PC端
{
    public class GetResidentFilesListCommandHandler : IRequestHandler<GetResidentFilesListCommand, ApiPaging<View_ResidentFileList>>
    {
        /// <summary>
        /// SqlSugar
        /// </summary>
        private readonly SqlSugarContext sqlSugar;
        private readonly IMapper mapper;

        public GetResidentFilesListCommandHandler(SqlSugarContext sqlSugar, IMapper mapper)
        {
            this.sqlSugar = sqlSugar;
            this.mapper = mapper;
        }

        public async Task<ApiPaging<View_ResidentFileList>> Handle(GetResidentFilesListCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<View_ResidentFileList> result = new ApiPaging<View_ResidentFileList>();
            var list = sqlSugar.db.Queryable<ResidentFiles>()
                .LeftJoin<NCDSign>((file, sign) => file.ResidentFiles_Id == sign.Patient_Id)//多个条件用&&
                .Select((file, sign) => new View_ResidentFileList
                {
                    ResidentFiles_Id = file.ResidentFiles_Id,
                    ResidentFiles_Name = file.ResidentFiles_Name,
                    Sex = file.Sex == true ? "男" : "女",
                    IdCard = file.IdCard,
                    Age = file.Age,
                    Address = file.Address,
                    Phone = file.Phone,
                    Birthday = file.Birthday.DateTimeToString("yyyy-MM-dd HH:mm:ss", "--"),
                    CreateTime = file.CreateTime,
                    Sign_Status = sign.Sign_Status,
                    Doctor = sign.Doctor,
                    Team = sign.Team,
                });
            if (request.ResidentFiles_Name.IsNotNull())
            {
                list = list.Where(x => x.ResidentFiles_Name.Contains(request.ResidentFiles_Name));
            }
            if(request.CreateStartTime.toString().IsNotNull() && request.CreateEndTime.toString().IsNotNull())
            {
                list = list.Where(x => x.CreateTime >= request.CreateStartTime && x.CreateTime < request.CreateEndTime.AddDays(1).AddMilliseconds(-1.0));
            }
            int TotalCount = list.Count();
            int TotalPage = (int)Math.Ceiling(TotalCount * 1.0 / request.PageSize);
            list = list.OrderByDescending(x => x.CreateTime).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            result.list = list.ToList();
            result.TotalCount = TotalCount;
            result.TotalPage = TotalPage;
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();
            return await Task.FromResult(result);
        }
    }
}
